package city;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import cn.imnaer.service.persistence.model.City;
import cn.imnaer.service.rpc.location.CityDaoImpl;

public class Parser {
    public void parse(String filename) throws Exception {
      List<City>  cities = new ArrayList<>();
        try (FileInputStream stream = new FileInputStream(filename);
            InputStreamReader iReader = new InputStreamReader(stream, "utf-8");
            BufferedReader reader = new BufferedReader(iReader)) {
            String line;
            while ((line = reader.readLine()) != null) {
            	System.out.println(line);
            City  city=    parseLine(line.trim());
            if(city!=null&&city.getCityCode()!=null && city.getCityName()!=null)
            cities.add(city);
            }           
        CityDaoImpl.getInstance().batchInsert(cities);   
       	System.out.println("执行完毕");
        }
    }

    public  City parseLine(String line) {
        int index = line.lastIndexOf(' ');
        if (index < 0) {
            return null;
        }
        City  city=new City();
        String regex = "\\s+";
        String strAry[] = line.split(regex);
            String  cityName = strAry[0];
            String  cityCode=strAry[1];
            city.setCityName(cityName);
            city.setCityCode(cityCode);    
            System.out.println(city);
        return city;
    }
     public static void main(String[] args) throws Exception {
		Parser parse=new Parser();
		String fileName="/Volumes/我的文件/城市/全国主要城市电话区号.txt";
		parse.parse(fileName);
	}
    }

